package com.atguigu.gulimall.ssoclient.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class HelloController {

    @ResponseBody
    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }

    @GetMapping("/employees")
    public String employees(HttpSession session, Model model,
                            @RequestParam(value = "token", required = false) String token) {
        // 拿到token之后说明登录成功了
        if (!StringUtils.isEmpty(token)) {
            // TODO: 2023/7/3 去ssoserver获取当前token真正对应的用户信息
            session.setAttribute("loginUser", "张三");
        }


        Object loginUser = session.getAttribute("loginUser");
        if (loginUser == null) {
            // 没登录，跳转到登录服务器登录
            // 拼接 redirect_url = https://client1.com:8081/employees，登录成功之后跳转回来
            return "redirect:http://ssoserver.com:8080/login.html?redirect_url=http://client1.com:8081/employees";
        } else {
            List<String> emps = new ArrayList<>();
            emps.add("张三");
            emps.add("李四");
            model.addAttribute("emps", emps);
            return "list";
        }
    }
}
